<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ～ ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\newsDeclare\excel;

use catchAdmin\newsDeclare\model\NewsDeclare as NewsDeclareModel;
use catcher\library\excel\ExcelContract;
use PhpOffice\PhpSpreadsheet\Style\Alignment;

class newsDeclareExport implements ExcelContract
{
    protected $model;


    public function __construct(NewsDeclareModel $NewsDeclareModel)
    {
        $this->model = $NewsDeclareModel;
    }
    /**
     * 设置头部
     *
     * @time 2020年09月08日
     * @return string[]
     */
    public function headers(): array
    {
        // TODO: Implement headers() method.
        return [
            '填报单位', '作品标题', '刊指媒体', '主创人员', '得票情况'
        ];
    }
    public function getPushInventoryAttr($value)
    {
        $status = [0=>'未推送',1=>'电子口岸已暂存',2=>'电子口岸申报中',3=>'发送海关成功',4=>'发送海关失败',100=>'海关退单',120=>'海关入库', 300=>'人工审核',399=>'海关审结',800=>'放行',899=>'结关',500=>'查验',501=>'扣留移送通关',502=>'扣留移送缉私',503=>'扣留移送法规',599=>'其它扣留',700=>'退运'];

        return array_key_exists($value,$status)?$status[$value]:$value;
    }

    public function sheets()
    {
        $list = $this->model->getExportList()->toArray();

        //return $list;

        foreach ($list as &$value){
            $_publication = '';
            if(!empty($value['publication_json'])){
                if( array_key_exists('publication_platform', $value['publication_json'])){
                    $_publication .= $value['publication_json']['publication_platform'];
                }
                if( array_key_exists('publication_layout', $value['publication_json'])){
                    $_publication  = $_publication . " " . $value['publication_json']['publication_layout'];
                }
            }
            $value['_publication'] = $_publication;
            //$value['_publication'] = $value['publication_json']['publication_platform'] . " " . $value['publication_json']['publication_layout'];
            $fields = ['rec_unit','news_title', '_publication','chief_creator','recommend_counts'];
            $_value = array_intersect_key($value, array_flip($fields));

            $value = array_combine($fields, array_map(function($key) use ($_value) {
                return $_value[$key];
            }, $fields));
        }


        //$list = array_column($list,['id']);

//        foreach ($list as $key => &$value){
//            $value->orderId .= "\t";
//            $value->ConsigneeCode .= "\t";
//            $value->push_inventory = $this->getPushInventoryAttr($value->push_inventory);
//        }
        return $list;
    }

    /**
     * 设置开始行
     *
     * @time 2020年09月08日
     * @return int
     */
    public function setRow()
    {
        return 2;
    }
    /**
     * 设置行宽
     *
     * @time 2020年09月08日
     * @return int
     */
    public function setWidth()
    {
        return [
            "A"=>30, "B"=>30, "C"=>30, "D"=>30, "E"=>20, "F"=>20, "G"=>15,"H"=>15,"I"=>25,"J"=>40
        ];
    }

    /**
     * 设置标题
     *
     * @time 2020年09月08日
     * @return array
     */
    public function setTitle()
    {
        return [
            'A1:J1', '新闻奖评审结果', Alignment::HORIZONTAL_CENTER
        ];
    }
}